package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.testng.annotations.DataProvider;

public class ContactDataGenerator {

	public static String[] MONTHS = new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	static Random rnd = new Random();
	
	@DataProvider (name = "randomContacts")
	public static Iterator<Object[]> generateRandomContacts() {
		List<ContactObject> contacts = new ContactDataGenerator().generateRandomContactList(5);
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactObject contact : contacts) {
			list.add(new Object[]{contact});
		}
		return list.iterator();
	}
	
	@DataProvider (name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException  {
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader(new FileReader("contacts.dat"));
		String line = reader.readLine();
		while (line != null) {
			String[] parts = line.split("\t");
			ContactObject contact = new ContactObject()
				.setFirstName(parts[0])
				.setLastName(parts[1])
				.setAddress(parts[2])
				.setHome(parts[3])
				.setMobile(parts[4])
				.setWork(parts[5])
				.setEmail(parts[6])
				.setEmail2(parts[7])
				.setBday(parts[8])
				.setBmonth(parts[9])
				.setByear(parts[10])
				.setNew_group(parts[11])
				.setAddress2(parts[12])
				.setPhone2(parts[13]);
			list.add(new Object[]{contact});
			line = reader.readLine();
		}
		return list.iterator();
	}

	public static void main(String[] args) {
		if (args.length <2) {
			System.out.println("Specify two parameters: file and count");
			return;
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]);
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactObject> contacts = generateRandomContactList(count);
		File file = new File(fileName);
		if (file.exists()) {
			//file.delete();
			System.out.println("File exists, stop generator");
			return;
		}
		FileWriter writer = new FileWriter(file);
		for (ContactObject contact : contacts) {
			writer.write(contact.firstname + "\t" + contact.lastname + "\t" + contact.address + "\t" + contact.home + "\t" + contact.mobile + "\t" + contact.work + "\t" + contact.email + "\t" + contact.email2 + "\t" + contact.bday + "\t" + contact.bmonth + "\t" + contact.byear + "\t" + contact.new_group + "\t" + contact.address2 + "\t" + contact.phone2 + "\n");
			writer.flush();
		}
		writer.close();
	}

	private List<ContactObject> generateRandomContactList(int count) {
		List<ContactObject> list = new ArrayList<ContactObject>();
		String month = MONTHS[new Random().nextInt(12-1)];
		for (int i = 0; i < count; i++) {
			ContactObject contact = new ContactObject()
				.setFirstName("fgddfg"+rnd.nextInt())
				.setLastName("dgdfgdf"+rnd.nextInt())
				.setAddress("dfgfdgdfgd"+rnd.nextInt())
				.setHome("495"+rnd.nextInt(9999999-1111111))
				.setMobile("916"+rnd.nextInt(9999999-1111111))
				.setWork("499"+rnd.nextInt(9999999-1111111))
				.setEmail("dfggdgfgdf@fgdfg.ru")
				.setEmail2("gdgfg@fghfghf.com")
				.setBday(""+rnd.nextInt(30-1))
				.setBmonth(month)
				.setByear("19"+rnd.nextInt(99-1)+1)
				.setNew_group("[none]")
				.setAddress2("dfgfghfhfhgfhfg"+rnd.nextInt())
				.setPhone2("495"+rnd.nextInt(9999999-1111111));
			list.add(contact);
		}
		return list;
	}
	
}
